#! /usr/bin/env python

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys, os

TITLE = 'ThumbGridWidget'
THUMBWIDTH = 64
PICS = ['./Elvis1.jpg',
		'./Elvis2.jpg',
		'./Elvis3.jpg',
		'./Elvis4.jpg']*4

black   = QColor(0, 0, 0)
white   = QColor(255, 255, 255)
red     = QColor(255, 0, 0)
green   = QColor(0, 255, 0)
blue    = QColor(0, 0, 255)
yellow  = QColor(255, 255, 0)
cyan    = QColor(0, 255, 255)
magenta = QColor(255, 0, 255)





class Thumb(QListWidgetItem):

	def __init__(self, parent=None, imgPath=''):
		super(Thumb, self).__init__(parent)

		pic = QPixmap(imgPath)
		icon = QIcon(pic)
		self.setIcon(icon)
		self.path = imgPath

	def printPath(self):
		print self.path

class ThumbListWidget(QListWidget):

	p = QPalette()


	def __init__(self, parent=None, palette=None):
		super(ThumbListWidget, self).__init__(parent)

		# Setup
		self.setViewMode(QListView.IconMode)
		self.setResizeMode(QListView.Adjust)
		self.setMovement(QListView.Static)
		self.setLayoutMode(QListView.SinglePass)
		self.setUniformItemSizes(1)
		self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
		self.setSelectionMode(QAbstractItemView.ExtendedSelection)

		# Colors
		if palette: self.p = palette
		self.setPalette(self.p)
		print "HEEEJ"


	def setBackgroundColor(self, color):
		self.p.setColor(QPalette.Base, color)
		self.setPalette(self.p)

	def setTextColor(self, color):
		self.p.setColor(QPalette.Text, color)
		self.setPalette(self.p)

	def addPictures(self, pictures):
		for pic in pictures:
			print pic
			imgName = pic.split("/")[-1]
			thumb = Thumb(None, pic, imgName)
			self.addItem(thumb)

	def addImages(self, images):
		for img in images:
			self.addItem(img)

	def selectionChanged (self, QItemSelection_selected, QItemSelection_deselected):
		pass

	def removeSelected(self):
		selected = self.selectedIndexes()
		for idx in selected:
			thumb = self.itemFromIndex(idx)
			if thumb:
				self.setCurrentItem(thumb)
				item = self.takeItem(self.currentRow())
				del item
	


class Main(QMainWindow):
	
	def __init__(self, parent=None):
		super(QMainWindow, self).__init__(parent)

		self.setWindowTitle(TITLE)

		self.listView = ThumbListWidget(self)

		#self.listView.setBackgroundColor(black)
		#self.listView.setTextColor(white)
		self.listView.setIconSize(QSize(THUMBWIDTH, THUMBWIDTH))
		self.listView.setGridSize(QSize(THUMBWIDTH+50, THUMBWIDTH+50))

		#self.listView.addPictures(PICS)

		self.setCentralWidget(self.listView)

	def keyPressEvent(self, event):
		if event.key() == Qt.Key_D:
			self.listView.removeSelected()
		elif event.key() == Qt.Key_Q:
			sys.exit(0)

def imageList(path):
	for subdir, dirs, files in os.walk(path):
		fPath = lambda file : path + "/" + file
		return map(fPath, files)

if __name__ == "__main__":
	PICS = imageList("../thumbnails")
	app = QApplication(sys.argv)
	win = Main()
	win.show()
	app.exec_()
